package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Controllor.DB;
import Model.Apartment;
import Model.Lease;
import Model.Employee;
import Model.Person;
import Model.Resident;

import javax.swing.JLabel;

import java.awt.FlowLayout;

import javax.swing.JButton;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;

public class ResidentForm extends JFrame {

	private JPanel contentPane;
	private Lease lease;
	private JTextField DOBFieldBox;
	private JTextField workPlaceFieldBox;
	private JTextField phoneFieldText;
	
	private List<Apartment> apartments;
	private List<Employee> employees;
	private List<Resident> residents;
	private DB database;
	private JTextField fNameTextField;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	
	
	/**
	 * @wbp.parser.constructor
	 */
	public ResidentForm(DB database) {
		this(null, database);
	}
	
	public ResidentForm(Lease lease, final DB database) {
		
		this.database = database;
		//Go away
		employees = database.getEmployees();
		residents = database.getResidents();
		if (lease != null)
			this.lease = lease;
		apartments = database.getApartments();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 460, 412);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		JButton btnNewButton = new JButton("Cancel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Submit");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String fname = fNameTextField.getText();
				String mInit = textField.getText();
				String lName = textField_1.getText();
				String dOB = DOBFieldBox.getText();
				int phone = Integer.parseInt(phoneFieldText.getText());
				String licensePlateNumber = textField_2.getText();
				String licensePlateState = textField_3.getText();
				String emergencyContactName = textField_5.getText();
				String emergencyContactNumber = textField_6.getText();
				
				String email = textField_7.getText();
				String licenseNumber = textField_8.getText();
				String licenseState = textField_9.getText();
				String workPlace = workPlaceFieldBox.getText();
				int workPhone = Integer.parseInt(textField_4.getText());
				int personid = 0;
				try {
					List<Person> persons = database.getPersons();
					personid = persons.get(persons.size() - 1).getID() + 1;
					database.addPerson(personid, fname, mInit, lName, dOB, phone, licensePlateNumber, licensePlateState, emergencyContactName, emergencyContactNumber);
				} catch (Exception e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				try {
					List<Resident> residents = database.getResidents();
					int resID = residents.get(residents.size() - 1).getID() + 1;
					database.addResident(resID, email, licenseNumber, licenseState, workPlace, workPhone, personid);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				dispose();
			}
		});
		panel.add(btnNewButton_1);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		JLabel lblStartDate = new JLabel("DOB");
		lblStartDate.setBounds(20, 67, 77, 14);
		panel_1.add(lblStartDate);
		
		DOBFieldBox = new JTextField();
		DOBFieldBox.setText("--/--/----");
		DOBFieldBox.setBounds(20, 80, 121, 20);
		DOBFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			DOBFieldBox.setText(lease.getStartDate());
			DOBFieldBox.setEditable(false);
		} else {
			DOBFieldBox.setText("--/--/----");	
		}
		
		panel_1.add(DOBFieldBox);
		DOBFieldBox.setColumns(10);


		
		JLabel lblResident = new JLabel("First Name");
		lblResident.setBounds(20, 21, 77, 14);
		panel_1.add(lblResident);

		
		workPlaceFieldBox = new JTextField();
		workPlaceFieldBox.setBounds(20, 307, 226, 20);
		workPlaceFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			workPlaceFieldBox.setText(lease.getRentersInsuranceCarrier() + "");
		}
		panel_1.add(workPlaceFieldBox);
		workPlaceFieldBox.setColumns(10);
		
		JLabel lblInsuranceCarrier = new JLabel("Work Place");
		lblInsuranceCarrier.setBounds(20, 293, 109, 14);
		panel_1.add(lblInsuranceCarrier);
		
		phoneFieldText = new JTextField();
		phoneFieldText.setText("#######");
		phoneFieldText.setBounds(150, 80, 177, 20);
		phoneFieldText.setColumns(10);
		phoneFieldText.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			phoneFieldText.setText(lease.getRentersInsuranceID() + "");
		}
		panel_1.add(phoneFieldText);

		
		fNameTextField = new JTextField();
		fNameTextField.setHorizontalAlignment(SwingConstants.CENTER);
		fNameTextField.setColumns(10);
		fNameTextField.setBounds(20, 36, 121, 20);
		panel_1.add(fNameTextField);
		
		JLabel lblM = new JLabel("M");
		lblM.setBounds(150, 21, 77, 14);
		panel_1.add(lblM);
		
		textField = new JTextField();
		textField.setHorizontalAlignment(SwingConstants.CENTER);
		textField.setColumns(10);
		textField.setBounds(151, 36, 78, 20);
		panel_1.add(textField);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(237, 21, 77, 14);
		panel_1.add(lblLastName);
		
		textField_1 = new JTextField();
		textField_1.setHorizontalAlignment(SwingConstants.CENTER);
		textField_1.setColumns(10);
		textField_1.setBounds(237, 36, 177, 20);
		panel_1.add(textField_1);
		
		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setBounds(150, 67, 77, 14);
		panel_1.add(lblPhone);
		
		textField_2 = new JTextField();
		textField_2.setHorizontalAlignment(SwingConstants.CENTER);
		textField_2.setColumns(10);
		textField_2.setBounds(20, 128, 226, 20);
		panel_1.add(textField_2);
		
		JLabel lblLicensePlateNumber = new JLabel("License Plate Number");
		lblLicensePlateNumber.setBounds(20, 111, 109, 14);
		panel_1.add(lblLicensePlateNumber);
		
		textField_3 = new JTextField();
		textField_3.setText("##");
		textField_3.setHorizontalAlignment(SwingConstants.CENTER);
		textField_3.setColumns(10);
		textField_3.setBounds(256, 128, 71, 20);
		panel_1.add(textField_3);
		
		JLabel lblState = new JLabel("State");
		lblState.setBounds(258, 111, 109, 14);
		panel_1.add(lblState);
		
		textField_4 = new JTextField();
		textField_4.setText("#######");
		textField_4.setHorizontalAlignment(SwingConstants.CENTER);
		textField_4.setColumns(10);
		textField_4.setBounds(256, 307, 168, 20);
		panel_1.add(textField_4);
		
		JLabel lblWorkPhone = new JLabel("Work Phone");
		lblWorkPhone.setBounds(258, 293, 109, 14);
		panel_1.add(lblWorkPhone);
		
		textField_5 = new JTextField();
		textField_5.setHorizontalAlignment(SwingConstants.CENTER);
		textField_5.setColumns(10);
		textField_5.setBounds(20, 174, 226, 20);
		panel_1.add(textField_5);
		
		JLabel lblEmergencyContact = new JLabel("Emergency Contact");
		lblEmergencyContact.setBounds(20, 159, 109, 14);
		panel_1.add(lblEmergencyContact);
		
		textField_6 = new JTextField();
		textField_6.setText("#######");
		textField_6.setHorizontalAlignment(SwingConstants.CENTER);
		textField_6.setColumns(10);
		textField_6.setBounds(256, 174, 168, 20);
		panel_1.add(textField_6);
		
		JLabel lblEmergencyPhone = new JLabel("Emergency Phone");
		lblEmergencyPhone.setBounds(258, 159, 109, 14);
		panel_1.add(lblEmergencyPhone);
		
		textField_7 = new JTextField();
		textField_7.setHorizontalAlignment(SwingConstants.CENTER);
		textField_7.setColumns(10);
		textField_7.setBounds(20, 219, 404, 20);
		panel_1.add(textField_7);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(20, 205, 77, 14);
		panel_1.add(lblEmail);
		
		textField_8 = new JTextField();
		textField_8.setHorizontalAlignment(SwingConstants.CENTER);
		textField_8.setColumns(10);
		textField_8.setBounds(20, 262, 226, 20);
		panel_1.add(textField_8);
		
		JLabel lblLicenseNumber = new JLabel("License Number");
		lblLicenseNumber.setBounds(20, 250, 109, 14);
		panel_1.add(lblLicenseNumber);
		
		textField_9 = new JTextField();
		textField_9.setText("##");
		textField_9.setHorizontalAlignment(SwingConstants.CENTER);
		textField_9.setColumns(10);
		textField_9.setBounds(256, 262, 71, 20);
		panel_1.add(textField_9);
		
		JLabel label_2 = new JLabel("State");
		label_2.setBounds(258, 250, 109, 14);
		panel_1.add(label_2);
		this.setVisible(true);
		
		
		
	}
}